import axios from "axios"
import auth from "./auth"
import router from "../routes"

const BASE_URL = "http://127.0.0.1:8000"
// const BASE_URL = "http://114.67.90.75:8082"


class Http {
  constructor() {
    this.http = axios.create({
      baseURL: BASE_URL
    })

    // 设置拦截器，用来添加JWT Token的
    this.http.interceptors.request.use(config => {
      const token = auth.token;
      if (token && token != 'undefined') {
        config.headers.common.Authorization = "JWT " + token
      }
      return config
    })

    // 设置拦截器，如果返回403，跳转到登陆页面
    // 403是没有权限访问这个接口,404是找不到接口地址,500是接口报错
    this.http.interceptors.response.use(null, err => {
      if (err && err.response) {
        if (err.response.status == 403) {
          auth.clearUserToken()
        } else if (err.response.status == 404){
          router.push({name:'error-404'})
        } else if (err.response.status == 500){
          router.push('/error-500')
        }
        return Promise.reject(err);
      }

    })
  }

  login(params) {
    const url = "/auth/login"
    return this.http.post(url, params)
  }

  addProject(params){
    const url = "/project"
    return this.http.post(url,params)
  }

  getProjectList(){
    const url = "/project"
    return this.http.get(url)
  }

  editProject(project_id,params){
    const url = "/project/" + project_id
    return this.http.put(url,params)
  }
  
  getProjectDetail(project_id){
    const url = "/project/" + project_id
    return this.http.get(url)
  }

  addHost(params){
    // name,host,description(可选),project_id
    const url = "/host"
    return this.http.post(url,params)
  }

  getHostList(){
    const url = "/host"
    return this.http.get(url)
  }

  deleteHost(host_id){
    const url="/host/"+host_id
    return this.http.delete(url)
  }

  editHost(host_id,params){
    const url = "/host/" + host_id
    return this.http.put(url,params)
  }

  addApi(params){
    const url = "/api"
    return this.http.post(url,params)
  }

  editApi(id,params){
    const url = "/api/" + id
    return this.http.put(url,params)
  }

  deleteApi(id){
    const url = "/api/" + id
    return this.http.delete(url)
  }

  runApi(id){
    const url = "/run/api/" + id
    return this.http.post(url)
  }

  addCase(params){
    const url = "/case"
    return this.http.post(url,params)
  }

  editCase(id,params){
    const url = "/case/" + id
    return this.http.put(url,params)
  }

  runCase(id){
    const url = "/run/case/"+id
    return this.http.post(url)
  }

  deleteCase(id){
    const url = "/case/" + id
    return this.http.delete(url)
  }

  getApiRecords(project_id){
    const url = "/records?project_id="+project_id+"&type=api"
    return this.http.get(url)
  }

  getCaseRecords(project_id) {
    const url = "/records?project_id=" + project_id + "&type=case"
    return this.http.get(url)
  }

  getUserList(){
    const url = '/auth/user'
    return this.http.get(url)
  }

  editUser(params){
    const url = "/auth/user"
    return this.http.put(url,params)
  }

  getIndexData(){
    const url = "/index"
    return this.http.get(url)
  }
}

export default new Http()